package com.mask.im.business.controller.friend;

import cn.hutool.json.JSONUtil;
import com.mask.im.business.annotation.AesCrypto;
import com.mask.im.business.common.model.PageResult;
import com.mask.im.business.model.dto.*;
import com.mask.im.business.model.entity.chat.IMFriendChatMessageDO;
import com.mask.im.business.model.vo.IMFriendChatMessageVO;
import com.mask.im.business.model.vo.RecommendVO;
import com.mask.im.business.common.model.RestResponse;
import com.mask.im.business.service.IMFriendChatMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 好友聊天管理
 *
 * @author jian
 */
@Log4j2
@Api(tags = "好友聊天记录管理")
@RestController
public class IMFriendChatMessageController {

    @Resource
    private IMFriendChatMessageService chatMessageService;

//    @AesCrypto
    @ApiOperation("发送消息")
    @PostMapping("/friend_chat/send")
    public RestResponse<IMFriendChatMessageDO> sendMessage(@RequestBody FriendSendMessageDTO sendMessageDTO) {
        return chatMessageService.sendMessage(sendMessageDTO);
    }

    @ApiOperation("撤回消息")
    @PostMapping("/friend_chat/retract/{id}")
    public RestResponse<IMFriendChatMessageDO> retractMessage(@PathVariable Long id) {
        return chatMessageService.retractMessage(id);
    }


    @ApiOperation("获取聊天记录")
    @GetMapping("/friend_chat/chatMessageList")
    public RestResponse<List<IMFriendChatMessageVO>> chatMessageList(@RequestParam("friendId") Long friendId) {
        return chatMessageService.chatMessageList(friendId);
    }

    /*****以下为原有接口,以上为重构所有接口*****/
    @ApiOperation("获取聊天记录-旧")
    @GetMapping("/friend_chat/getChatMessageList")
    public RestResponse<List<FriendChatMessageDTO>> getChatMessageList(@RequestParam("friendId") Long friendId) {
        return chatMessageService.getChatMessageList(friendId);
    }


    @ApiOperation("清空聊天记录")
    @DeleteMapping("/friend_chat/clearChatMessage")
    public RestResponse<String> clearChatMessage(@RequestParam("friendId") Long friendId) {
        return chatMessageService.clearChatMessage(friendId);
    }

    @AesCrypto
    @ApiOperation("消息已读")
    @PostMapping("/friend_chat/markMessageRead")
    public RestResponse<?> messageRead(@RequestBody MessageReadDTO messageReadDTO) {
        log.info("friend_chat============markMessageRead=====>[{}]", JSONUtil.toJsonPrettyStr(messageReadDTO));
        return chatMessageService.messageRead(messageReadDTO);
    }

    @ApiOperation("删除聊天记录")
    @DeleteMapping("/friend_chat/deleteChatMessage/{chatId}")
    public RestResponse<Integer> deleteChatMessage(@PathVariable Integer chatId) {
        return chatMessageService.deleteChatMessage(chatId);
    }


    @AesCrypto
    @ApiOperation("发送推荐的用户明信片")
    @PostMapping("/friend_chat/sendRecommend")
    public RestResponse<String> sendRecommendFriends(@RequestBody RecommendVO recommendVO) {
        return chatMessageService.sendRecommendFriends(recommendVO);
    }

    /*******************************************后台接口***************************************************/

    @ApiOperation("获取聊天列表")
    @GetMapping("/admin/chatMessage")
    public PageResult<AdminChatMessageDTO> chatMessage(@RequestParam(value = "sender", defaultValue = "") String sender,
                                                       @RequestParam(value = "receiver", defaultValue = "") String receiver) {
        return chatMessageService.chatMessage(sender, receiver);
    }


    @ApiOperation("获取某条聊天内容")
    @GetMapping("/admin/viewContent/{type}")
    public RestResponse<String> viewContent(
            @PathVariable String type,
            @RequestParam(value = "password", defaultValue = "") String password,
            @RequestParam(value = "id", defaultValue = "") Integer id) {
        return chatMessageService.viewContent(id, password, type);
    }
}

